/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.core.model;

import java.io.Serializable;

/**
 * Contains info about a country, such as name and country code.
 * 
 * @see #getName()
 * @see #getCode()
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class Country implements Serializable {

	private static final long serialVersionUID = -58122374802013567L;

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public final class Editor {

		/**
		 * private constructor
		 */
		private Editor() {
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			Country.this.name = name == null ? DEFAULT_NAME : name;
		}

		/**
		 * @param code
		 *            the country code to set
		 */
		public void setCountryCode(String code) {
			Country.this.code = code == null ? DEFAULT_CODE : code;
		}

	}

	public static final String DEFAULT_NAME = "Untitled Country";
	public static final String DEFAULT_CODE = "XX";

	/** the name of the country */
	private String name;

	/** country code */
	private String code;

	transient private Editor editor;

	/**
	 * default constructor
	 */
	public Country() {
		this(null, null);
	}

	/**
	 * @param code
	 * @param name
	 */
	public Country(String code, String name) {
		this.code = code == null ? DEFAULT_CODE : code;
		this.name = name == null ? DEFAULT_NAME : name;
	}

	/**
	 * @return
	 */
	Editor getEditor() {
		if (editor == null) {
			editor = new Editor();
		}

		return editor;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the countryCode
	 */
	public String getCode() {
		return code;
	}

	@Override
	public String toString() {
		return code + " : " + name;
	}

	@Override
	public int hashCode() {
		return code.hashCode() + name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		Country another;
		if (obj instanceof Country) {
			another = (Country) obj;
		} else {
			return false;
		}

		if (!name.equals(another.name)) {
			return false;
		}
		if (!code.equals(another.code)) {
			return false;
		}

		return true;
	}

}
